package com.sskjdata.cg.controller.auth;

import com.sskjdata.cg.annotation.NoLog;
import com.sskjdata.cg.auth.JwtAuthenticationRequest;
import com.sskjdata.cg.dto.ResponseCodeEnum;
import com.sskjdata.cg.dto.ResponseData;
import com.sskjdata.cg.service.auth.AuthService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Descirption
 * @Author Jason
 * @Date 2019/11/30/
 */
@RestController
@Slf4j
public class AuthController {
    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private AuthService authService;

    @Autowired
    private AuthenticationManager authenticationManager;
    /**
     * 获取token
     * @param authenticationRequest
     * @return
     * @throws AuthenticationException
     */

    @ApiOperation("授权获取token")
    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    public ResponseData createAuthenticationToken(
            @RequestBody JwtAuthenticationRequest authenticationRequest) throws AuthenticationException {
        try {
            String token = authService.login(authenticationManager,authenticationRequest.getUsername(), authenticationRequest.getPassword());
            // Return the token
            return new ResponseData(ResponseCodeEnum.SUCCESS,token);
        } catch (Exception e) {
            if(!(e instanceof AuthenticationException)){// 用户名密码验证错误
                log.error("获取token出错",e);
                return new ResponseData(ResponseCodeEnum.ERROR);
            }
        }

        return new ResponseData(ResponseCodeEnum.USERNAME_OR_PASSWORD_NOT_TRUE);
    }

    /**
     * 刷新token
     * @param request
     * @return
     * @throws AuthenticationException
     */
    @ApiOperation("刷新token")
    @NoLog
    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    public ResponseData refreshAndGetAuthenticationToken(
            HttpServletRequest request) throws AuthenticationException{
        try {
            String token = request.getHeader(tokenHeader);
            String refreshedToken = authService.refresh(token);
            if(refreshedToken == null) {
                return new ResponseData(ResponseCodeEnum.FAILURE);
            } else {
                return new ResponseData(ResponseCodeEnum.SUCCESS,refreshedToken);
            }
        } catch (Exception e) {
            log.error("刷新token出错",e);
        }
        return new ResponseData(ResponseCodeEnum.ERROR);
    }

}

